package com.lvmama.controller.rhino.staticcode;

import com.github.pagehelper.PageInfo;
import com.lvmama.client.rhino.StatisticCodeClient;
import com.lvmama.pojo.dto.rhino.topic.StatisticCodesConditionVO;
import com.lvmama.pojo.entity.rhino.topic.StatisticCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @param
 * @Title:
 * @Author:wangpeng
 * @Description: TODO(这里用一句话描述这个方法的作用)
 * @return
 * @throws
 */
@Controller
@RequestMapping("statisticCode")
@Slf4j
public class StatisticCodeController {

	@Autowired
	StatisticCodeClient codeClient;

	/**
	 * code码列表列表
	 *
	 * @param model
	 * @param sc
	 * @param message
	 * @return
	 */
	@RequestMapping("list")
	public String list(ModelMap model, StatisticCodesConditionVO sc, String message) {
		PageInfo<StatisticCode> page = codeClient.getAllList(sc);
		if(sc == null) sc = new StatisticCodesConditionVO();
		model.addAttribute("statisticCodesConditionVO", sc);
		model.addAttribute("page", page);
		model.addAttribute("statisticCodes", page.getList());
		model.addAttribute("message", message);
		return "rhino/staticcode/statisticCodeList";
	}

	/**
	 * 编辑code码
	 * @param codeId
	 * @return
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public Map<String, Object> edit(String codeId) {
		return codeClient.edit(codeId).getData();
	}

	/**
	 * code码树形展示
	 *
	 * @param model
	 * @return
	 */
	@RequestMapping("new")
	public String newStatisticCode(Model model, String message) {
		model.addAttribute("message", message);
		String str = codeClient.treeviewStr().getData();
		model.addAttribute("treeview", str);
		return "rhino/staticcode/statisticCodeForm";
	}

	/**
	 * 操作code码操作
	 * @return
	 */
	@RequestMapping("save")
	@ResponseBody
	Map<String, Object> saveCode(StatisticCode statisticCode, Integer parentId, boolean isNew){
		return codeClient.saveCode(statisticCode,parentId,isNew).getData();
	}

	/**
	 * 获取jQueryEasyUI的tree的数据
	 *
	 * @param parentName
	 * @return
	 */
	@RequestMapping(value = "treegrid", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String statisticCode(String parentName){
		return codeClient.treegrid(parentName).getData();
	}

	@RequestMapping("saveCode")
	@ResponseBody
	Map<String, Object> saveCodeTree( String codeInfo){
		return codeClient.saveCodeTree(codeInfo).getData();
	}

	/**
	 * 随机生成code码
	 * @param parentCode
	 * @return
	 */
	@RequestMapping("createCode")
	@ResponseBody
	Map<String, Object> createCode(String parentCode){
		return codeClient.createCode(parentCode).getData();
	}

	/**
	 * 随机生成看坑位code码
	 * @param page
	 * @param pitName
	 * @return
	 */
	@RequestMapping("createPitCode")
	@ResponseBody
	Map<String, Object> createPitCode( String page,String pitName){
		return codeClient.createPitCode(page,pitName).getData();
	}

	/**
	 * 查询父级code码信息
	 *
	 * @return
	 */
	@RequestMapping("/getParent")
	@ResponseBody
	Map<String, Object> getParent(String nodeHtml){
		return codeClient.getParent(nodeHtml).getData();
	}

	@RequestMapping("getUserMenu")
	@ResponseBody
	Map<String, Object> getUserMenu(){
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("code", 1);
		result.put("msg", "该code码不能创建子级code码！");
		return result;
	}

}
